/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-05-16     shelton      first version
 */

#include "drv_common.h"
#include "drv_usartX.h"
#include "drv_config.h"

#ifdef RT_USING_SERIAL
#if !defined(BSP_USING_UART1) && !defined(BSP_USING_UART2) && \
    !defined(BSP_USING_UART3) && !defined(BSP_USING_UART4) && \
    !defined(BSP_USING_UART5) && !defined(BSP_USING_UART6) && \
    !defined(BSP_USING_UART7) && !defined(BSP_USING_UART8)
    #error "Please define at least one BSP_USING_UARTx"
#endif

enum {
#ifdef BSP_USING_UART1
    UART1_INDEX,
#endif
#ifdef BSP_USING_UART2
    UART2_INDEX,
#endif
#ifdef BSP_USING_UART3
    UART3_INDEX,
#endif
#ifdef BSP_USING_UART4
    UART4_INDEX,
#endif
#ifdef BSP_USING_UART5
    UART5_INDEX,
#endif
#ifdef BSP_USING_UART6
    UART6_INDEX,
#endif
#ifdef BSP_USING_UART7
    UART7_INDEX,
#endif
#ifdef BSP_USING_UART8
    UART8_INDEX,
#endif
};

static struct at32_uart uart_config[] = {
#ifdef BSP_USING_UART1
    UART1_CONFIG,
#endif
#ifdef BSP_USING_UART2
    UART2_CONFIG,
#endif
#ifdef BSP_USING_UART3
    UART3_CONFIG,
#endif
#ifdef BSP_USING_UART4
    UART4_CONFIG,
#endif
#ifdef BSP_USING_UART5
    UART5_CONFIG,
#endif
#ifdef BSP_USING_UART6
    UART6_CONFIG,
#endif
#ifdef BSP_USING_UART7
    UART7_CONFIG,
#endif
#ifdef BSP_USING_UART8
    UART8_CONFIG,
#endif
};

#ifdef RT_SERIAL_USING_DMA
static void at32_dma_rx_config(struct rt_serial_device *serial);
static void at32_dma_tx_config(struct rt_serial_device *serial);
#endif

static rt_err_t at32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
    struct at32_uart *instance;
    usart_data_bit_num_type data_bit;
    usart_stop_bit_num_type stop_bit;
    usart_parity_selection_type parity_mode;

    RT_ASSERT(serial != RT_NULL);
    RT_ASSERT(cfg != RT_NULL);

    instance = rt_container_of(serial, struct at32_uart, serial);
    RT_ASSERT(instance != RT_NULL);

    at32_msp_usart_init((void *)instance);

    switch (cfg->data_bits) {
    case DATA_BITS_8:
        data_bit = USART_DATA_8BITS;
        break;
    case DATA_BITS_9:
        data_bit = USART_DATA_9BITS;
        break;
    default:
        data_bit = USART_DATA_8BITS;
        break;
    }

    switch (cfg->stop_bits) {
    case STOP_BITS_1:
        stop_bit = USART_STOP_1_BIT;
        break;
    case STOP_BITS_2:
        stop_bit = USART_STOP_2_BIT;
        break;
    default:
        stop_bit = USART_STOP_1_BIT;
        break;
    }

    switch (cfg->parity) {
    case PARITY_NONE:
        parity_mode = USART_PARITY_NONE;
        break;
    case PARITY_ODD:
        parity_mode = USART_PARITY_ODD;
        break;
    case PARITY_EVEN:
        parity_mode = USART_PARITY_EVEN;
        break;
    default:
        parity_mode = USART_PARITY_NONE;
        break;
    }
    usart_parity_selection_config(instance->uart_x, parity_mode);
    usart_init(instance->uart_x, cfg->baud_rate, data_bit, stop_bit);

    return RT_EOK;
}

static rt_err_t at32_init(struct rt_serial_device *serial)
{
    struct at32_uart *instance;

    instance = rt_container_of(serial, struct at32_uart, serial);

    if (at32_configure(serial, &serial->config) != RT_EOK)
    {
        return -RT_ERROR;
    }

    usart_receiver_enable(instance->uart_x, TRUE);
    usart_transmitter_enable(instance->uart_x, TRUE);

    usart_hardware_flow_control_set(instance->uart_x, USART_HARDWARE_FLOW_NONE);
    usart_enable(instance->uart_x, TRUE);

    return RT_EOK;
}

static rt_err_t at32_control(struct rt_serial_device *serial, int cmd, void *arg)
{
    struct at32_uart *instance;
    rt_ubase_t ctrl_arg = (rt_ubase_t)arg;

    RT_ASSERT(serial != RT_NULL);
    instance = rt_container_of(serial, struct at32_uart, serial);
    RT_ASSERT(instance != RT_NULL);

    switch (cmd) {
    case RT_DEVICE_CTRL_OPEN:
        usart_interrupt_enable(instance->uart_x, USART_TDBE_INT, FALSE);
        usart_flag_clear(instance->uart_x, USART_RDBF_FLAG);
        usart_flag_clear(instance->uart_x, USART_TDBE_FLAG);
        usart_flag_clear(instance->uart_x, USART_TDC_FLAG);
        nvic_irq_enable(instance->irqn, 2, 1);
    break;
    case RT_DEVICE_CTRL_CLOSE:
        nvic_irq_disable(instance->irqn);
#ifdef RT_SERIAL_USING_DMA
        nvic_irq_disable(instance->dma_rx.dma_irqn);
        nvic_irq_disable(instance->dma_tx.dma_irqn);
#endif
        usart_interrupt_enable(instance->uart_x, USART_RDBF_INT, FALSE);
        usart_interrupt_enable(instance->uart_x, USART_TDC_INT, FALSE);
        usart_interrupt_enable(instance->uart_x, USART_TDBE_INT, FALSE);
        usart_enable(instance->uart_x, FALSE);
    break;
    case RT_DEVICE_CTRL_CLR_INT:
        /* disable interrupt */
        if (ctrl_arg & RT_DEVICE_FLAG_INT_RX) {
            usart_interrupt_enable(instance->uart_x, USART_RDBF_INT, FALSE);
        }

#ifdef RT_SERIAL_USING_DMA
        /* disable DMA */
        if (ctrl_arg == RT_DEVICE_FLAG_DMA_RX)
        {
            nvic_irq_disable(instance->dma_rx->dma_irqn);
            dma_reset(instance->dma_rx->dma_channel);
        }
        else if(ctrl_arg == RT_DEVICE_FLAG_DMA_TX)
        {
            nvic_irq_disable(instance->dma_tx->dma_irqn);
            dma_reset(instance->dma_tx->dma_channel);
        }
#endif
    break;
    case RT_DEVICE_CTRL_SET_INT:
        /* enable rx irq */
        if (ctrl_arg & RT_DEVICE_FLAG_INT_RX) {
            usart_interrupt_enable(instance->uart_x, USART_RDBF_INT, TRUE);
        }
    break;
#ifdef RT_SERIAL_USING_DMA
    case RT_DEVICE_CTRL_CONFIG:
        if (ctrl_arg & RT_DEVICE_FLAG_DMA_RX) {
            at32_dma_rx_config(serial);
        } else if (ctrl_arg & RT_DEVICE_FLAG_DMA_TX) {
            at32_dma_tx_config(serial);
        }
    break;
#endif
    default :
    break;
    }

    return RT_EOK;
}

static int at32_putc(struct rt_serial_device *serial, char ch)
{
    struct at32_uart *instance;

    RT_ASSERT(serial != RT_NULL);
    instance = rt_container_of(serial, struct at32_uart, serial);
    RT_ASSERT(instance != RT_NULL);

    while (usart_flag_get(instance->uart_x, USART_TDBE_FLAG) == RESET);   // wait for tx empty
    usart_data_transmit(instance->uart_x, (uint8_t)ch);

    return 1;
}

static int at32_getc(struct rt_serial_device *serial)
{
    int ch;
    struct at32_uart *instance;

    RT_ASSERT(serial != RT_NULL);
    instance = rt_container_of(serial, struct at32_uart, serial);
    RT_ASSERT(instance != RT_NULL);

    ch = -1;
    if (usart_flag_get(instance->uart_x, USART_RDBF_FLAG) != RESET) {
        ch = usart_data_receive(instance->uart_x) & 0xff;
    }

    return ch;
}

static int at32_flush(struct rt_serial_device *serial)
{
    struct at32_uart *instance;

    RT_ASSERT(serial != RT_NULL);
    instance = rt_container_of(serial, struct at32_uart, serial);
    RT_ASSERT(instance != RT_NULL);

    while (usart_flag_get(instance->uart_x, USART_TDC_FLAG) == RESET);

    return 1;
}

static void at32_start_tx(struct rt_serial_device *serial)
{
    struct at32_uart *instance;

    instance = rt_container_of(serial, struct at32_uart, serial);

    usart_interrupt_enable(instance->uart_x, USART_TDBE_INT, TRUE); // 使能发送空中断
}

static void at32_stop_tx(struct rt_serial_device *serial)
{
    struct at32_uart *instance;

    instance = rt_container_of(serial, struct at32_uart, serial);

    usart_interrupt_enable(instance->uart_x, USART_TDBE_INT, FALSE); // 禁止发送空中断
}

#ifdef RT_SERIAL_USING_DMA
static rt_bool_t at32_is_dma_txing(struct rt_serial_device *serial)
{
    struct at32_uart *instance;

    RT_ASSERT(serial != RT_NULL);

    instance = rt_container_of(serial, struct at32_uart, serial);

    return instance->dmaTxing;  //RT_FALSE;
}

static void at32_start_dma_tx(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size)
{
    struct at32_uart *instance;
    HAL_StatusTypeDef status;
    DMA_HandleTypeDef *hdma;

    RT_ASSERT(serial != RT_NULL);

    instance = rt_container_of(serial, struct at32_uart, serial);

    /* wait before transfer complete */
    while(instance->dma_tx->dma_done == RT_FALSE);

    dma_channel_type *dma_channel = instance->dma_tx->dma_channel;

    dma_channel->dtcnt = size;
    dma_channel->paddr = (rt_uint32_t)&(instance->uart_x->dt);
    dma_channel->maddr = (rt_uint32_t)buf;

    /* enable transmit complete interrupt */
    dma_interrupt_enable(dma_channel, DMA_FDT_INT, TRUE);
    /* enable dma transmit */
    usart_dma_transmitter_enable(instance->uart_x, TRUE);

    /* mark dma flag */
    instance->dma_tx->dma_done = RT_FALSE;
    /* enable dma channel */
    dma_channel_enable(dma_channel, TRUE);

    instance->dmaTxing = RT_TRUE;
}

static void at32_stop_dma_tx(struct rt_serial_device *serial)
{
    struct at32_uart *instance;

    RT_ASSERT(serial != RT_NULL);

    instance = rt_container_of(serial, struct at32_uart, serial);

    /* enable transmit complete interrupt */
    dma_interrupt_enable(dma_channel, DMA_FDT_INT, FALSE);
    /* enable dma transmit */
    usart_dma_transmitter_enable(instance->uart_x, FALSE);

    /* mark dma flag */
    instance->dma_tx->dma_done = RT_TRUE;
    /* enable dma channel */
    dma_channel_enable(dma_channel, FALSE);
    instance->dmaTxing = RT_FALSE;
}
#endif

static void at32_enable_interrupt(struct rt_serial_device *serial)
{
}

static void at32_disable_interrupt(struct rt_serial_device *serial)
{
}

static void at32_dma_rx_config(struct rt_serial_device *serial)
{
    dma_init_type dma_init_struct;
    dma_channel_type *dma_channel = NULL;
    struct rt_serial_rx_fifo *rx_fifo;
    struct at32_uart *instance;
    struct dma_config *dma_config;

    RT_ASSERT(serial != RT_NULL);
    instance = (struct at32_uart *) serial->parent.user_data;
    RT_ASSERT(instance != RT_NULL);

    dma_channel = instance->dma_rx->dma_channel;
    dma_config = instance->dma_rx;

    crm_periph_clock_enable(dma_config->dma_clock, TRUE);
    dma_default_para_init(&dma_init_struct);
    dma_init_struct.peripheral_inc_enable = FALSE;
    dma_init_struct.memory_inc_enable = TRUE;
    dma_init_struct.peripheral_data_width = DMA_PERIPHERAL_DATA_WIDTH_BYTE;
    dma_init_struct.memory_data_width = DMA_MEMORY_DATA_WIDTH_BYTE;
    dma_init_struct.priority = DMA_PRIORITY_MEDIUM;

    dma_init_struct.direction = DMA_DIR_PERIPHERAL_TO_MEMORY;
    dma_init_struct.loop_mode_enable = TRUE;

    dma_reset(dma_channel);
    dma_init(dma_channel, &dma_init_struct);
#if defined (SOC_SERIES_AT32F425)
    dma_flexible_config(dma_config->dma_x, dma_config->flex_channel, \
                       (dma_flexible_request_type)dma_config->request_id);
#endif
#if defined (SOC_SERIES_AT32F435) || defined (SOC_SERIES_AT32F437)
    dmamux_enable(dma_config->dma_x, TRUE);
    dmamux_init(dma_config->dmamux_channel, (dmamux_requst_id_sel_type)dma_config->request_id);
#endif
    /* enable interrupt */
    /* start dma transfer */
    _uart_dma_receive(instance, serial->serial_dma_rx, RT_SERIAL_DMA_BUFSZ);

    /* dma irq should set in dma tx mode */
    nvic_irq_enable(dma_config->dma_irqn, 0, 0);
}

static void at32_dma_tx_config(struct rt_serial_device *serial)
{
    dma_init_type dma_init_struct;
    dma_channel_type *dma_channel = NULL;
    struct rt_serial_rx_fifo *rx_fifo;
    struct at32_uart *instance;
    struct dma_config *dma_config;

    RT_ASSERT(serial != RT_NULL);
    instance = (struct at32_uart *) serial->parent.user_data;
    RT_ASSERT(instance != RT_NULL);

    dma_channel = instance->dma_tx->dma_channel;
    dma_config = instance->dma_tx;

    crm_periph_clock_enable(dma_config->dma_clock, TRUE);
    dma_default_para_init(&dma_init_struct);
    dma_init_struct.peripheral_inc_enable = FALSE;
    dma_init_struct.memory_inc_enable = TRUE;
    dma_init_struct.peripheral_data_width = DMA_PERIPHERAL_DATA_WIDTH_BYTE;
    dma_init_struct.memory_data_width = DMA_MEMORY_DATA_WIDTH_BYTE;
    dma_init_struct.priority = DMA_PRIORITY_MEDIUM;

    dma_init_struct.direction = DMA_DIR_MEMORY_TO_PERIPHERAL;
    dma_init_struct.loop_mode_enable = FALSE;

    dma_reset(dma_channel);
    dma_init(dma_channel, &dma_init_struct);
#if defined (SOC_SERIES_AT32F425)
    dma_flexible_config(dma_config->dma_x, dma_config->flex_channel, \
                       (dma_flexible_request_type)dma_config->request_id);
#endif
#if defined (SOC_SERIES_AT32F435) || defined (SOC_SERIES_AT32F437)
    dmamux_enable(dma_config->dma_x, TRUE);
    dmamux_init(dma_config->dmamux_channel, (dmamux_requst_id_sel_type)dma_config->request_id);
#endif

    /* dma irq should set in dma tx mode */
    nvic_irq_enable(dma_config->dma_irqn, 0, 0);
}

static const struct rt_uart_ops at32_uart_ops = {
    .init = at32_init,
    .configure = at32_configure,
    .control = at32_control,
    .putc = at32_putc,
    .getc = at32_getc,
    .flush = at32_flush,
    .start_tx = at32_start_tx,
    .stop_tx = at32_stop_tx,
#ifdef RT_SERIAL_USING_DMA
    .is_dma_txing = at32_is_dma_txing,
    .start_dma_tx = at32_start_dma_tx,
    .stop_dma_tx = at32_stop_dma_tx,
#endif
    .enable_interrupt = at32_enable_interrupt,
    .disable_interrupt = at32_disable_interrupt,
};

#ifdef RT_SERIAL_USING_DMA
void dma_rx_isr(struct rt_serial_device *serial)
{
    volatile rt_uint32_t reg_sts = 0, index = 0;
    rt_size_t dma_cnt;
    struct at32_uart *instance;

    instance = rt_container_of(serial, struct at32_uart, serial);
    RT_ASSERT(instance != RT_NULL);

    reg_sts = instance->dma_rx->dma_x->sts;
    index = instance->dma_rx->channel_index;

    if (((reg_sts & (DMA_FDT_FLAG << (4 * (index - 1)))) != RESET) ||
        ((reg_sts & (DMA_HDT_FLAG << (4 * (index - 1)))) != RESET))
    {
        /* clear dma flag */
        instance->dma_rx->dma_x->clr |= (rt_uint32_t)(DMA_FDT_FLAG << (4 * (index - 1))) | (DMA_HDT_FLAG << (4 * (index - 1)));

        dma_cnt = RT_SERIAL_DMA_BUFSZ - dma_data_number_get(instance->dma_rx->dma_channel);

        if (dma_cnt)
        {
            rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | (dma_cnt << 8));
        }
    }
}

void dma_tx_isr(struct rt_serial_device *serial)
{
    volatile rt_uint32_t reg_sts = 0, index = 0;
    rt_size_t trans_total_index;
    rt_base_t level;
    RT_ASSERT(serial != RT_NULL);
    struct at32_uart *instance;
    instance = (struct at32_uart *) serial->parent.user_data;
    RT_ASSERT(instance != RT_NULL);

    reg_sts = instance->dma_tx->dma_x->sts;
    index = instance->dma_tx->channel_index;

    if ((reg_sts & (DMA_FDT_FLAG << (4 * (index - 1)))) != RESET)
    {
        /* mark dma flag */
        instance->dma_tx->dma_done = RT_TRUE;
        /* clear dma flag */
        instance->dma_tx->dma_x->clr |= (rt_uint32_t)(DMA_FDT_FLAG << (4 * (index - 1)));
        /* disable dma tx channel */
        dma_channel_enable(instance->dma_tx->dma_channel, FALSE);

        level = rt_hw_interrupt_disable();
        trans_total_index = dma_data_number_get(instance->dma_tx->dma_channel);
        rt_hw_interrupt_enable(level);

        if (trans_total_index == 0)
        {
            rt_hw_serial_isr(serial, RT_SERIAL_EVENT_TX_DMADONE);
        }
    }
}
#endif

static void usart_isr(struct rt_serial_device *serial)
{
    struct at32_uart *instance;
#ifdef RT_SERIAL_USING_DMA
    rt_size_t dma_cnt;
#endif

    RT_ASSERT(serial != RT_NULL);

    instance = rt_container_of(serial, struct at32_uart, serial);
    RT_ASSERT(instance != RT_NULL);

    if (usart_flag_get(instance->uart_x, USART_RDBF_FLAG) != RESET) {
        rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
        usart_flag_clear(instance->uart_x, USART_RDBF_FLAG);
    }
    if (usart_flag_get(instance->uart_x, USART_TDC_FLAG) != RESET) {
        rt_hw_serial_isr(serial, RT_SERIAL_EVENT_TX_DONE);
        usart_flag_clear(instance->uart_x, USART_TDC_FLAG);
    }
#ifdef RT_SERIAL_USING_DMA
    else if (usart_flag_get(instance->uart_x, USART_IDLEF_FLAG) != RESET)
    {
        /* clear idle flag */
        usart_data_receive(instance->uart_x);

        dma_cnt = RT_SERIAL_DMA_BUFSZ - dma_data_number_get(instance->dma_rx->dma_channel);

        if (recv_len)
        {
            rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | (dma_cnt << 8));
        }
    }
#endif
    else
    {
        if (usart_flag_get(instance->uart_x, USART_CTSCF_FLAG) != RESET) {
            usart_flag_clear(instance->uart_x, USART_CTSCF_FLAG);
        }

        if (usart_flag_get(instance->uart_x, USART_BFF_FLAG) != RESET) {
            usart_flag_clear(instance->uart_x, USART_BFF_FLAG);
        }
    }
}

#ifdef BSP_USING_UART1
void USART1_IRQHandler(void) {
    rt_interrupt_enter();

    usart_isr(&uart_config[UART1_INDEX].serial);

    rt_interrupt_leave();
}
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_RX_USING_DMA)
void UART1_RX_DMA_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    dma_rx_isr(&uart_config[UART1_INDEX].serial);

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_RX_USING_DMA) */
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_TX_USING_DMA)
void UART1_TX_DMA_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    dma_tx_isr(&uart_config[UART1_INDEX].serial);

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_TX_USING_DMA) */
#endif
#ifdef BSP_USING_UART2
void USART2_IRQHandler(void) {
    rt_interrupt_enter();

    usart_isr(&uart_config[UART2_INDEX].serial);

    rt_interrupt_leave();
}
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART2_RX_USING_DMA)
void UART2_RX_DMA_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    dma_rx_isr(&uart_config[UART2_INDEX].serial);

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART2_RX_USING_DMA) */
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART2_TX_USING_DMA)
void UART2_TX_DMA_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    dma_tx_isr(&uart_config[UART2_INDEX].serial);

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART2_TX_USING_DMA) */
#endif
#ifdef BSP_USING_UART3
void USART3_IRQHandler(void) {
    rt_interrupt_enter();

    usart_isr(&uart_config[UART3_INDEX].serial);

    rt_interrupt_leave();
}
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART3_RX_USING_DMA)
void UART3_RX_DMA_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    dma_rx_isr(&uart_config[UART3_INDEX].serial);

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART3_RX_USING_DMA) */
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART3_TX_USING_DMA)
void UART3_TX_DMA_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    dma_tx_isr(&uart_config[UART3_INDEX].serial);

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART3_TX_USING_DMA) */
#endif
#ifdef BSP_USING_UART4
void UART4_IRQHandler(void) {
    rt_interrupt_enter();

    usart_isr(&uart_config[UART4_INDEX].serial);

    rt_interrupt_leave();
}
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART4_RX_USING_DMA)
void UART4_RX_DMA_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    dma_rx_isr(&uart_config[UART4_INDEX].serial);

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART4_RX_USING_DMA) */
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART4_TX_USING_DMA)
void UART4_TX_DMA_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    dma_tx_isr(&uart_config[UART4_INDEX].serial);

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART14_TX_USING_DMA) */
#endif
#ifdef BSP_USING_UART5
void UART5_IRQHandler(void) {
    rt_interrupt_enter();

    usart_isr(&uart_config[UART5_INDEX].serial);

    rt_interrupt_leave();
}
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART5_RX_USING_DMA)
void UART5_RX_DMA_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    dma_rx_isr(&uart_config[UART5_INDEX].serial);

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART5_RX_USING_DMA) */
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART5_TX_USING_DMA)
void UART5_TX_DMA_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    dma_tx_isr(&uart_config[UART5_INDEX].serial);

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART5_TX_USING_DMA) */
#endif
#ifdef BSP_USING_UART6
void USART6_IRQHandler(void) {
    rt_interrupt_enter();

    usart_isr(&uart_config[UART6_INDEX].serial);

    rt_interrupt_leave();
}
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART6_RX_USING_DMA)
void UART6_RX_DMA_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    dma_rx_isr(&uart_config[UART6_INDEX].serial);

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART6_RX_USING_DMA) */
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART6_TX_USING_DMA)
void UART6_TX_DMA_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    dma_tx_isr(&uart_config[UART6_INDEX].serial);

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART6_TX_USING_DMA) */
#endif
#ifdef BSP_USING_UART7
void UART7_IRQHandler(void) {
    rt_interrupt_enter();

    usart_isr(&uart_config[UART7_INDEX].serial);

    rt_interrupt_leave();
}
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART7_RX_USING_DMA)
void UART7_RX_DMA_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    dma_rx_isr(&uart_config[UART7_INDEX].serial);

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART7_RX_USING_DMA) */
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART7_TX_USING_DMA)
void UART7_TX_DMA_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    dma_tx_isr(&uart_config[UART7_INDEX].serial);

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART7_TX_USING_DMA) */
#endif
#ifdef BSP_USING_UART8
void UART8_IRQHandler(void) {
    rt_interrupt_enter();

    usart_isr(&uart_config[UART8_INDEX].serial);

    rt_interrupt_leave();
}
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART8_RX_USING_DMA)
void UART8_RX_DMA_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    dma_rx_isr(&uart_config[UART8_INDEX].serial);

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART8_RX_USING_DMA) */
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART8_TX_USING_DMA)
void UART8_TX_DMA_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    dma_tx_isr(&uart_config[UART8_INDEX].serial);

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* defined(RT_SERIAL_USING_DMA) && defined(BSP_UART8_TX_USING_DMA) */
#endif

#if defined (SOC_SERIES_AT32F421)
void UART1_TX_RX_DMA_IRQHandler(void)
{
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_TX_USING_DMA)
    UART1_TX_DMA_IRQHandler();
#endif

#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_RX_USING_DMA)
    UART1_RX_DMA_IRQHandler();
#endif
}

void UART2_TX_RX_DMA_IRQHandler(void)
{
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART2_TX_USING_DMA)
    UART2_TX_DMA_IRQHandler();
#endif

#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART2_RX_USING_DMA)
    UART2_RX_DMA_IRQHandler();
#endif
}
#endif

#if defined (SOC_SERIES_AT32F425)
#if defined(BSP_USING_UART3) || defined(BSP_USING_UART4)
void USART4_3_IRQHandler(void)
{
#if defined(BSP_USING_UART3)
  USART3_IRQHandler();
#endif
#if defined(BSP_USING_UART4)
  UART4_IRQHandler();
#endif
}
#endif

void UART1_TX_RX_DMA_IRQHandler(void)
{
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_TX_USING_DMA)
    UART1_TX_DMA_IRQHandler();
#endif

#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART1_RX_USING_DMA)
    UART1_RX_DMA_IRQHandler();
#endif
}

void UART3_2_TX_RX_DMA_IRQHandler(void)
{
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART2_TX_USING_DMA)
    UART2_TX_DMA_IRQHandler();
#endif

#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART2_RX_USING_DMA)
    UART2_RX_DMA_IRQHandler();
#endif
#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART3_TX_USING_DMA)
    UART3_TX_DMA_IRQHandler();
#endif

#if defined(RT_SERIAL_USING_DMA) && defined(BSP_UART3_RX_USING_DMA)
    UART3_RX_DMA_IRQHandler();
#endif
}
#endif

#ifdef RT_SERIAL_USING_DMA
static void _dma_base_channel_check(struct at32_uart *instance)
{
    dma_channel_type *rx_channel = instance->dma_rx->dma_channel;
    dma_channel_type *tx_channel = instance->dma_tx->dma_channel;

    instance->dma_rx->dma_done = RT_TRUE;
    instance->dma_rx->dma_x = (dma_type *)((rt_uint32_t)rx_channel & ~0xFF);
    instance->dma_rx->channel_index = ((((rt_uint32_t)rx_channel & 0xFF) - 8) / 0x14) + 1;

    instance->dma_tx->dma_done = RT_TRUE;
    instance->dma_tx->dma_x = (dma_type *)((rt_uint32_t)tx_channel & ~0xFF);
    instance->dma_tx->channel_index = ((((rt_uint32_t)tx_channel & 0xFF) - 8) / 0x14) + 1;
}

static void at32_uart_get_dma_config(void)
{
#ifdef BSP_USING_UART1
    uart_config[UART1_INDEX].uart_dma_flag = 0;
#ifdef BSP_UART1_RX_USING_DMA
    uart_config[UART1_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
    static struct dma_config uart1_dma_rx = UART1_RX_DMA_CONFIG;
    uart_config[UART1_INDEX].dma_rx = &uart1_dma_rx;
#endif
#ifdef BSP_UART1_TX_USING_DMA
    uart_config[UART1_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
    static struct dma_config uart1_dma_tx = UART1_TX_DMA_CONFIG;
    uart_config[UART1_INDEX].dma_tx = &uart1_dma_tx;
#endif
#endif

#ifdef BSP_USING_UART2
    uart_config[UART2_INDEX].uart_dma_flag = 0;
#ifdef BSP_UART2_RX_USING_DMA
    uart_config[UART2_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
    static struct dma_config uart2_dma_rx = UART2_RX_DMA_CONFIG;
    uart_config[UART2_INDEX].dma_rx = &uart2_dma_rx;
#endif
#ifdef BSP_UART2_TX_USING_DMA
    uart_config[UART2_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
    static struct dma_config uart2_dma_tx = UART2_TX_DMA_CONFIG;
    uart_config[UART2_INDEX].dma_tx = &uart2_dma_tx;
#endif
#endif

#ifdef BSP_USING_UART3
    uart_config[UART3_INDEX].uart_dma_flag = 0;
#ifdef BSP_UART3_RX_USING_DMA
    uart_config[UART3_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
    static struct dma_config uart3_dma_rx = UART3_RX_DMA_CONFIG;
    uart_config[UART3_INDEX].dma_rx = &uart3_dma_rx;
#endif
#ifdef BSP_UART3_TX_USING_DMA
    uart_config[UART3_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
    static struct dma_config uart3_dma_tx = UART3_TX_DMA_CONFIG;
    uart_config[UART3_INDEX].dma_tx = &uart3_dma_tx;
#endif
#endif

#ifdef BSP_USING_UART4
    uart_config[UART4_INDEX].uart_dma_flag = 0;
#ifdef BSP_UART4_RX_USING_DMA
    uart_config[UART4_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
    static struct dma_config uart4_dma_rx = UART4_RX_DMA_CONFIG;
    uart_config[UART4_INDEX].dma_rx = &uart4_dma_rx;
#endif
#ifdef BSP_UART4_TX_USING_DMA
    uart_config[UART4_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
    static struct dma_config uart4_dma_tx = UART4_TX_DMA_CONFIG;
    uart_config[UART4_INDEX].dma_tx = &uart4_dma_tx;
#endif
#endif

#ifdef BSP_USING_UART5
    uart_config[UART5_INDEX].uart_dma_flag = 0;
#ifdef BSP_UART5_RX_USING_DMA
    uart_config[UART5_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
    static struct dma_config uart5_dma_rx = UART5_RX_DMA_CONFIG;
    uart_config[UART5_INDEX].dma_rx = &uart5_dma_rx;
#endif
#ifdef BSP_UART5_TX_USING_DMA
    uart_config[UART5_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
    static struct dma_config uart5_dma_tx = UART5_TX_DMA_CONFIG;
    uart_config[UART5_INDEX].dma_tx = &uart5_dma_tx;
#endif
#endif

#ifdef BSP_USING_UART6
    uart_config[UART6_INDEX].uart_dma_flag = 0;
#ifdef BSP_UART6_RX_USING_DMA
    uart_config[UART6_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
    static struct dma_config uart6_dma_rx = UART6_RX_DMA_CONFIG;
    uart_config[UART6_INDEX].dma_rx = &uart6_dma_rx;
#endif
#ifdef BSP_UART6_TX_USING_DMA
    uart_config[UART6_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
    static struct dma_config uart6_dma_tx = UART6_TX_DMA_CONFIG;
    uart_config[UART6_INDEX].dma_tx = &uart6_dma_tx;
#endif
#endif

#ifdef BSP_USING_UART7
    uart_config[UART7_INDEX].uart_dma_flag = 0;
#ifdef BSP_UART7_RX_USING_DMA
    uart_config[UART7_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
    static struct dma_config uart7_dma_rx = UART7_RX_DMA_CONFIG;
    uart_config[UART7_INDEX].dma_rx = &uart7_dma_rx;
#endif
#ifdef BSP_UART7_TX_USING_DMA
    uart_config[UART7_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
    static struct dma_config uart7_dma_tx = UART7_TX_DMA_CONFIG;
    uart_config[UART7_INDEX].dma_tx = &uart7_dma_tx;
#endif
#endif

#ifdef BSP_USING_UART8
    uart_config[UART8_INDEX].uart_dma_flag = 0;
#ifdef BSP_UART8_RX_USING_DMA
    uart_config[UART8_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_RX;
    static struct dma_config uart8_dma_rx = UART8_RX_DMA_CONFIG;
    uart_config[UART8_INDEX].dma_rx = &uart8_dma_rx;
#endif
#ifdef BSP_UART8_TX_USING_DMA
    uart_config[UART8_INDEX].uart_dma_flag |= RT_DEVICE_FLAG_DMA_TX;
    static struct dma_config uart8_dma_tx = UART8_TX_DMA_CONFIG;
    uart_config[UART8_INDEX].dma_tx = &uart8_dma_tx;
#endif
#endif
}
#endif

int rt_hw_usart_init(void) {
    rt_size_t obj_num;
    int index;

    obj_num = sizeof(uart_config) / sizeof(struct at32_uart);
    struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
    rt_err_t result = 0;

#ifdef RT_SERIAL_USING_DMA
    at32_uart_get_dma_config();
#endif

    for (index = 0; index < obj_num; index++) {
        uart_config[index].serial.ops = &at32_uart_ops;
        uart_config[index].serial.config = config;

#if defined (RT_SERIAL_USING_DMA)
        /* search dma base and channel index */
        _dma_base_channel_check(&uart_config[index]);
#endif

        /* register uart device */
        result = rt_hw_serial_register(&uart_config[index].serial,
                 uart_config[index].name,
                 RT_DEVICE_FLAG_RDWR
                 | RT_DEVICE_FLAG_INT_RX
                 | RT_DEVICE_FLAG_INT_TX
#ifdef RT_SERIAL_USING_DMA
                 | uart_config[index].uart_dma_flag ,
#endif
                 NULL);
        RT_ASSERT(result == RT_EOK);
    }

    return result;
}

#endif /* BSP_USING_SERIAL */
